﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ares.Controller;
using Ares.Engine;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Forms;
using Ares.GUI.Controls;
using System.IO;
using Ares.GUI.Models;

namespace Ares.GUI.Controller
{
    public class NewProjectController : BaseController
    {
        private BackupProject currentProject;
        private ICommand saveCommand;
        private ICommand cancelCommand;

        private ICommand setDestinationCommand;
        private ICommand setSourceCommand;

        //Old Stuff
        private string projectComment;
        private string projectDestination;
        private string projectSource;
        private Guid projectGuid;
        private string projectStorageEngine;
        private string projectName;
        private BackupMethod projectMethod;

        public string ProjectComment
        {
            get
            {
                return projectComment;
            }
            set
            {
                projectComment = value;
                OnPropertyChanged("ProjectComment");
            }
        }

        public string ProjectDestination
        {
            get
            {
                return projectDestination;
            }
            set
            {
                projectDestination = value;
                OnPropertyChanged("ProjectDestination");
            }
        }

        public string ProjectSource
        {
            get
            {
                return projectSource;
            }
            set
            {
                projectSource = value;
                OnPropertyChanged("ProjectSource");
            }
        }

        public string ProjectStorageEngine
        {
            get
            {
                return projectStorageEngine;
            }
            set
            {
                projectStorageEngine = value;
                OnPropertyChanged("ProjectStorageEngine");
            }
        }

        public string ProjectName
        {
            get
            {
                return projectName;
            }
            set
            {
                projectName = value;
                OnPropertyChanged("ProjectName");
            }
        }

        public Guid ProjectGuid
        {
            get
            {
                return projectGuid;
            }
            set
            {
                projectGuid = value;
                OnPropertyChanged("ProjectGuid");
            }
        }

        public BackupMethod ProjectMethod
        {
            get
            {
                return projectMethod;
            }
            set
            {
                projectMethod = value;
                OnPropertyChanged("ProjectMethod");
            }
        }
        
        public BackupProject CurrentProject
        {
            get
            {
                return currentProject;
            }
            set
            {
                currentProject = value;
                OnPropertyChanged("CurrentProject");
            }
        }

        public ICommand SetDestinationCommand
        {
            get
            {
                if (this.setDestinationCommand == null)
                    this.setDestinationCommand = new RelayCommand(p => this.SetDestination(p), () => this.CanSetDestination());

                return this.setDestinationCommand;
            }
        }

        public ICommand SetSourceCommand
        {
            get
            {
                if (this.setSourceCommand == null)
                    this.setSourceCommand = new RelayCommand(p => this.SetSource(p), () => this.CanSetSource());

                return this.setSourceCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (this.saveCommand == null)
                    this.saveCommand = new RelayCommand(p => this.Save(p), () => this.CanSave());

                return this.saveCommand;
            }
        }

 

        public ICommand CancelCommand
        {
            get
            {
                if (this.cancelCommand == null)
                    this.cancelCommand = new RelayCommand(p => this.Cancel(p), () => this.CanCancel());

                return this.cancelCommand;
            }
        }

        public List<BackupMethod> BackupMethods { get; set; }
        public List<string> BackupEngines { get; set; }

        private void SetBackupMethod()
        {
            Array values = Enum.GetValues((typeof(BackupMethod)));

            BackupMethods = new List<BackupMethod>();
            foreach (BackupMethod value in values)
            {
                BackupMethods.Add(value);
            }
        }

        private void SetBackupEngine()
        {
            using (Backup Engine = new Backup())
            {
                BackupEngines = Engine.GetAllExtensionNames();
            }
        }

        public NewProjectController()
        {
            Init();
            Mediator.Register(this, new[]{
                Messages.EditProject,
                Messages.DoNewProject
            });
        }

        public void Init(){
            SetBackupMethod();
            SetBackupEngine();
            CurrentProject = new BackupProject();
            CurrentProject.Method = BackupMethod.full;
            CurrentProject.ProjectGuid = Guid.NewGuid();
            if (BackupEngines.Count > 0)
                CurrentProject.StorageEngine = BackupEngines[0];
            OnPropertyChanged("CurrentProject");

        }

        private void Save(object p)
        {
            //Gnaaaaaah!!
            CurrentProject.Comment = ProjectComment;
            CurrentProject.Destination = ProjectDestination;
            CurrentProject.Source = ProjectSource;
            CurrentProject.ProjectGuid = ProjectGuid;
            CurrentProject.StorageEngine = ProjectStorageEngine;
            CurrentProject.Name = ProjectName;
            CurrentProject.Method = ProjectMethod;
            OnPropertyChanged("CurrentProject");
            Mediator.NotifyColleagues(Messages.SaveProject, currentProject);
        }

        private bool CanSave()
        {
            return !String.IsNullOrEmpty(ProjectName) && 
                    Directory.Exists(ProjectSource) &&
                    Directory.Exists(ProjectDestination);
        }

        private void Cancel(object p)
        {

            Mediator.NotifyColleagues(Messages.CancelNewProject, CurrentProject);
        }

        private bool CanCancel()
        {
            return true;
        }

        private void SetSource(object p)
        {
            
            var dlg1 = new FolderBrowserDialogEx
            {
                Description = "Select a source for your backup:",
                ShowNewFolderButton = true,
                ShowEditBox = true,
                //NewStyle = false,
                SelectedPath = CurrentProject.Source,
                ShowFullPathInEditBox = false,
            };
            dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;

            var result = dlg1.ShowDialog();

            if (result == DialogResult.OK)
            {
                ProjectSource = dlg1.SelectedPath;
                //OnPropertyChanged("CurrentProject");
            }
        }

        private bool CanSetSource()
        {
            return true;
        }

        private void SetDestination(object p)
        {
            var dlg1 = new FolderBrowserDialogEx
            {
                Description = "Select a destination for your backup:",
                ShowNewFolderButton = true,
                ShowEditBox = true,
                //NewStyle = false,
                SelectedPath = CurrentProject.Source,
                ShowFullPathInEditBox = false,
            };
            dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;

            var result = dlg1.ShowDialog();

            if (result == DialogResult.OK)
            {
                ProjectDestination = dlg1.SelectedPath;
                //OnPropertyChanged("CurrentProject");
            }
        }

        private bool CanSetDestination()
        {
            return true;
        }


        public override void MessageNotification(string message, object args)
        {
            switch(message)
            {
                case Messages.EditProject:
                    BackupContainer backupCon = (BackupContainer)args;

                    //Gnaaaaaah!!
                    ProjectComment = backupCon.Project.Comment;
                    ProjectDestination = backupCon.Project.Destination;
                    ProjectSource =backupCon.Project.Source;
                    ProjectGuid = backupCon.Project.ProjectGuid;
                    ProjectStorageEngine = backupCon.Project.StorageEngine;
                    ProjectName = backupCon.Project.Name;
                    ProjectMethod = backupCon.Project.Method;

                    CurrentProject = backupCon.Project;
                    break;

                case Messages.DoNewProject:
                    Init();

                    ProjectComment = CurrentProject.Comment;
                    ProjectDestination = CurrentProject.Destination;
                    ProjectSource = CurrentProject.Source;
                    ProjectGuid = CurrentProject.ProjectGuid;
                    ProjectStorageEngine = CurrentProject.StorageEngine;
                    ProjectName = CurrentProject.Name;
                    ProjectMethod = CurrentProject.Method;
                    break;
            }
        }

        public string OldProject { get; set; }
    }
}
